/**
 * 
 */
package com.nwpusp.model;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

/**
 * @author JustinCao
 *
 */
public class EntityContainer extends Vector<Entity> {
	private static final long serialVersionUID = 1127L;
	private class XAxisCompator implements Comparator {
		@Override
		public int compare(Object o1, Object o2) {
			Entity e1 = (Entity)o1;
			Entity e2 = (Entity)o2;
			if(e1.getPosition().getX() > e2.getPosition().getX()) {
				return 1;
			}
			else if(e1.getPosition().getX() < e2.getPosition().getX()) {
				return -1;
			}
			else {
				if(e1.getPosition().getY() > e2.getPosition().getY()) {
					return 1;
				}
				else if(e1.getPosition().getY() < e2.getPosition().getY()) {
					return -1;
				}
				return 0;
			}
		}
	}
	
	private XAxisCompator comparator = new XAxisCompator();

	public boolean add(Entity e) {
		boolean result = super.add(e);
		if(result) {
			Sort();
		}
		return result;
	}
	
	public void Sort() {
		Collections.sort(this, comparator);
	}
}
